package com.mew.slots.core.game.hrljj.service;

import com.mew.common.enm.SlotsWinType;
import com.mew.common.entity.game.WinFree;
import com.mew.common.exception.ResponseCode;
import com.mew.common.util.GameUtil;
import com.mew.slots.core.common.messge.response.SlotsBonusPlayResponseMessage;
import com.mew.slots.core.common.utils.SlotsGameUtil;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.core.game.hrljj.message.HrljjBonusPlayResponseMessage;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HrljjBonusService {
    private static final int[] ITEM={10,15,20,30,40,50,60,80,-1,-2,-3,-4};//选项
    private static final int[] ITEM_WEIGHTS={50,50,150,500,500,400,300,200,10,5,2,1};//选项权重
    public SlotsBonusPlayResponseMessage play(StrategyGameVO player){
        SlotsBonusPlayResponseMessage message=new SlotsBonusPlayResponseMessage();
        WinFree winFree=player.getWinFree();
        if(winFree.getBounsNumber() < 1){
            player.setStat(ResponseCode.BounsCanNotPlay.code());
            return message;
        }

        player.setWinType(SlotsWinType.Special.code());
        player.setInBetNumber(winFree.getBet());

        HrljjBonusPlayResponseMessage hrljjBonusPlayResponseMessage = playGame(player);
        winFree.setBounsNumber(0);
        return hrljjBonusPlayResponseMessage;
    }

    /**
     * @param player
     * @return
     */
    private HrljjBonusPlayResponseMessage playGame(StrategyGameVO player){
        HrljjBonusPlayResponseMessage message=new HrljjBonusPlayResponseMessage();
        int randomIndex= GameUtil.weights(ITEM_WEIGHTS);
        int randomResult = ITEM[randomIndex];
        if(randomResult<0) {// 结果特殊
            int type = Math.abs(randomResult);
            message.setType(type);
            message.setJpNo(type);
            player.setWinType(SlotsWinType.Jackpot.code());
        }else{
            message.setType(0);
            message.setRate(randomResult);
            message.setPayment(player.getWinFree().getBet()*player.getLineNumber()*randomResult);
            player.setPaymentNumber(message.getPayment());
        }
        player.setIconResult(String.valueOf(randomResult));
        return message;
    }

    public static void main(String[] args) {
        StrategyGameVO vo = SlotsGameUtil.buildTestStrategyGameVO();
        vo.setLineNumber(5);
        WinFree winFree = vo.getWinFree();
        int rate = 0;
        for (int i = 0; i < 10000000; i++) {
            winFree.setBounsNumber(1);
            HrljjBonusPlayResponseMessage res =(HrljjBonusPlayResponseMessage) new HrljjBonusService().play(vo);
            rate += res.getRate();
        }
        System.out.println(rate/10000000);
    }
}
